{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "58ca4bc4-6b08-4a73-8b3a-44f3ec172b2b",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 不同形状的矩阵转置\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7249514f-dc2b-4a17-80e0-4a81f04b23c0",
   "metadata": {},
   "source": [
    "这段代码主要研究不同类型的向量和矩阵，并分析它们的转置性质。代码从基本的行向量、列向量到更复杂的矩阵（细高矩阵、扁平矩阵、方阵、对称矩阵）逐步展开，并使用 `.T` 进行转置操作。下面是详细的数学分析：\n",
    "\n",
    "---\n",
    "\n",
    "### **1. 行向量（1D 数组）**\n",
    "代码首先定义了一个一维 NumPy 数组：\n",
    "$$\n",
    "\\text{row\\_vector\\_1d} = \\begin{bmatrix} 1 & 2 & 3 & 4 \\end{bmatrix}\n",
    "$$\n",
    "在 NumPy 中，这是一个 **1D 数组**，其形状为 $(4,)$。尝试对其执行 `.T` 操作不会改变形状，因为 NumPy 认为它是一个 **扁平数组**，而不是严格意义上的矩阵。因此：\n",
    "$$\n",
    "\\text{row\\_vector\\_1d}^T = \\text{row\\_vector\\_1d}\n",
    "$$\n",
    "这表明对于 **1D 数组而言，转置没有作用**。\n",
    "\n",
    "---\n",
    "\n",
    "### **2. 行向量（2D 数组）**\n",
    "为了保证转置的正确性，我们使用二维数组：\n",
    "$$\n",
    "\\text{row\\_vector\\_2d} = \\begin{bmatrix} 1 & 2 & 3 & 4 \\end{bmatrix}\n",
    "$$\n",
    "其形状为 $(1,4)$。此时，对其执行 `.T` 转置操作，会变为列向量：\n",
    "$$\n",
    "\\text{row\\_vector\\_2d}^T =\n",
    "\\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\\\ 4 \\end{bmatrix}\n",
    "$$\n",
    "转置后的形状变为 $(4,1)$，即一个 **列向量**。\n",
    "\n",
    "---\n",
    "\n",
    "### **3. 列向量（2D 数组）**\n",
    "定义一个 **列向量**：\n",
    "$$\n",
    "\\text{col\\_vector} =\n",
    "\\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\\\ 4 \\end{bmatrix}\n",
    "$$\n",
    "其形状为 $(4,1)$。执行 `.T` 之后：\n",
    "$$\n",
    "\\text{col\\_vector}^T =\n",
    "\\begin{bmatrix} 1 & 2 & 3 & 4 \\end{bmatrix}\n",
    "$$\n",
    "转置后的形状变为 $(1,4)$，即变为一个 **行向量**。\n",
    "\n",
    "---\n",
    "\n",
    "### **4. 细高矩阵（Tall Matrix）**\n",
    "代码定义了一个 **细高矩阵**，即行数大于列数的矩阵：\n",
    "$$\n",
    "\\text{tall\\_matrix} =\n",
    "\\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\\\ 5 & 6 \\end{bmatrix}\n",
    "$$\n",
    "形状为 $(3,2)$。执行 `.T` 转置后：\n",
    "$$\n",
    "\\text{tall\\_matrix}^T =\n",
    "\\begin{bmatrix} 1 & 3 & 5 \\\\ 2 & 4 & 6 \\end{bmatrix}\n",
    "$$\n",
    "转置后的形状变为 $(2,3)$，行数和列数发生互换。\n",
    "\n",
    "---\n",
    "\n",
    "### **5. 扁平矩阵（Wide Matrix）**\n",
    "定义一个 **扁平矩阵**，即列数大于行数：\n",
    "$$\n",
    "\\text{wide\\_matrix} =\n",
    "\\begin{bmatrix} 1 & 2 & 3 & 4 \\\\ 5 & 6 & 7 & 8 \\end{bmatrix}\n",
    "$$\n",
    "形状为 $(2,4)$。执行 `.T` 转置后：\n",
    "$$\n",
    "\\text{wide\\_matrix}^T =\n",
    "\\begin{bmatrix} 1 & 5 \\\\ 2 & 6 \\\\ 3 & 7 \\\\ 4 & 8 \\end{bmatrix}\n",
    "$$\n",
    "转置后的形状变为 $(4,2)$，行列互换。\n",
    "\n",
    "---\n",
    "\n",
    "### **6. 方阵（Square Matrix）**\n",
    "方阵是行数等于列数的矩阵：\n",
    "$$\n",
    "\\text{square\\_matrix} =\n",
    "\\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9 \\end{bmatrix}\n",
    "$$\n",
    "形状为 $(3,3)$。执行 `.T` 转置后：\n",
    "$$\n",
    "\\text{square\\_matrix}^T =\n",
    "\\begin{bmatrix} 1 & 4 & 7 \\\\ 2 & 5 & 8 \\\\ 3 & 6 & 9 \\end{bmatrix}\n",
    "$$\n",
    "转置仍然是一个 $(3,3)$ 的方阵，但主对角线之外的元素发生了交换。\n",
    "\n",
    "---\n",
    "\n",
    "### **7. 对称矩阵（Symmetric Matrix）**\n",
    "定义一个 **对称矩阵**：\n",
    "$$\n",
    "\\text{symmetric\\_matrix} =\n",
    "\\begin{bmatrix} 1 & 2 & 3 \\\\ 2 & 4 & 5 \\\\ 3 & 5 & 6 \\end{bmatrix}\n",
    "$$\n",
    "它的转置为：\n",
    "$$\n",
    "\\text{symmetric\\_matrix}^T =\n",
    "\\begin{bmatrix} 1 & 2 & 3 \\\\ 2 & 4 & 5 \\\\ 3 & 5 & 6 \\end{bmatrix}\n",
    "$$\n",
    "由于 $A^T = A$，所以该矩阵是**对称矩阵**。\n",
    "\n",
    "---\n",
    "\n",
    "### **总结**\n",
    "1. **1D 数组（行向量）** 在 NumPy 中不能正确转置，仍然是 1D 数组。\n",
    "2. **2D 行向量** 可以正确转置，变为列向量。\n",
    "3. **2D 列向量** 也可以正确转置，变为行向量。\n",
    "4. **矩阵的转置** 是将行和列互换：\n",
    "   - 细高矩阵的转置变为扁平矩阵。\n",
    "   - 扁平矩阵的转置变为细高矩阵。\n",
    "   - 方阵的转置仍然是方阵，但元素交换。\n",
    "5. **对称矩阵** 满足 $A^T = A$，因此转置后不变。\n",
    "\n",
    "这些操作是线性代数和数据科学中的基础概念，广泛应用于机器学习、信号处理和图像分析中。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f1e3e2b-cbd2-446b-a725-f63eecbfa420",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2592b60b-7a9f-4b19-98f6-9333fc3545a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c90500cd-1931-4c70-8ae1-6c051c6034e6",
   "metadata": {},
   "source": [
    "## 行向量（1D 数组）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5be33057-414b-4a63-b7cc-ab7342d7473c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_vector_1d = np.array([1, 2, 3, 4])  # 一维数组（行向量）\n",
    "row_vector_1d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7a083ffc-9d73-481f-bfa6-119fe87bfcc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_vector_1d.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb9434a5-ec7a-462f-87ac-a3901fdb4c3e",
   "metadata": {},
   "source": [
    "## 行向量（2D 数组）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "167d1d7f-1452-4165-b4dc-016807c86e8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_vector_2d = np.array([[1, 2, 3, 4]])  # 2D 行向量\n",
    "row_vector_2d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5495a8d5-512b-4b27-8e85-12b772c54f35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_vector_2d.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cacf9643-e1b0-4e0e-b4b1-a307cbb6b7f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 列向量（2D 数组）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f7cce2e7-eeeb-4908-97db-6346a0d37405",
   "metadata": {},
   "outputs": [],
   "source": [
    "col_vector = np.array([[1], [2], [3], [4]]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "360425ec-d5e5-42b3-b95d-1a08c83ac64c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col_vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3946c3d0-0690-4e63-ad00-9ec06e848046",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col_vector.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2842fcc5-b9f9-4676-a531-6ab70c497101",
   "metadata": {},
   "source": [
    "## 细高矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5e3e8700-0d61-4582-bdc8-e9423dc3615b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tall_matrix = np.array([[1, 2], \n",
    "                        [3, 4], \n",
    "                        [5, 6]])\n",
    "tall_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f9c04a87-a3d2-4a4c-8908-c78eb4bd418c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3, 5],\n",
       "       [2, 4, 6]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tall_matrix.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5f597f81-9592-4ce5-b12b-47f2889a5c02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4],\n",
       "       [5, 6, 7, 8]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 扁平矩阵（2x4）\n",
    "wide_matrix = np.array([[1, 2, 3, 4], \n",
    "                        [5, 6, 7, 8]])\n",
    "wide_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0eb107a9-8243-4722-91b4-7b76b5b04124",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 5],\n",
       "       [2, 6],\n",
       "       [3, 7],\n",
       "       [4, 8]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wide_matrix.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f49a125c-7b83-4f02-b463-19485a362639",
   "metadata": {},
   "source": [
    "## 方阵（3x3）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6b482dc8-9e72-48f1-84c2-b763d0417fc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "square_matrix = np.array([[1, 2, 3], \n",
    "                          [4, 5, 6], \n",
    "                          [7, 8, 9]])\n",
    "square_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ffd3d002-f45a-4cdd-8ff9-0e93e3d7ba9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4, 7],\n",
       "       [2, 5, 8],\n",
       "       [3, 6, 9]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "square_matrix.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96a8a1a7-1ef1-4ae7-a1be-5bde7e23ad99",
   "metadata": {},
   "source": [
    "## 对称矩阵（3x3）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9494105f-d673-44bb-a13f-e1f139e18f40",
   "metadata": {},
   "outputs": [],
   "source": [
    "symmetric_matrix = np.array([[1, 2, 3], \n",
    "                             [2, 4, 5], \n",
    "                             [3, 5, 6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "dcbb0b7c-255d-4b08-acaf-0f75952c500f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 4, 5],\n",
       "       [3, 5, 6]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "symmetric_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a19bb85e-29bf-48f3-bed4-66978ed720a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 4, 5],\n",
       "       [3, 5, 6]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "symmetric_matrix.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e290a9c3-7710-4f85-9f96-bf748b5a619e",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
